package raojt.day0109.tour.test;

import raojt.day0109.tour.dao.TourDao;
import raojt.day0109.tour.dao.impl.TourDaoImpl;
import raojt.day0109.tour.entity.ScenicSpot;
import raojt.day0109.tour.entity.TourRoute;
import raojt.day0109.tour.entity.TourScenicrelation;

import java.util.*;

public class TourIndex {
    Map<String, Object> map = new HashMap<>();
    TourDao tourDao = new TourDaoImpl();
    Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        TourIndex tourIndex = new TourIndex();
        Scanner sc = new Scanner(System.in);
        boolean bool = true;
        do {
            System.out.println("请选择功能：1、查询信息 2、添加信息 3、删除信息 4、修改信息 q、退出程序");
            String choice = sc.nextLine();
            switch (choice) {
                case "1":
                    tourIndex.select();
                    break;
                case "2":
                    tourIndex.addTour();
                    break;
                case "3":
                    tourIndex.deleteTour();
                    break;
                case "4":
                    tourIndex.updateTour();
                    break;
                case "q":
                    bool = false;
                    break;
                default:
                    System.out.println("输入有误，请重新输入");
                    break;
            }
        } while (bool);

    }

    //查询信息方法
    public void select() {
        boolean bool = true;
        do {
            System.out.println("请选择功能：1、查询所有景点信息 2、查询所有旅游路线信息 3、根据ID查询景点信息  \n\t\t 4、根据名称查询景点信息 5、根据ID查询旅游路线信息 6、根据名称查询旅游路线信息 \n\t\t 7、查询关系表信息 8、通过景点名称查看旅游路线 q、返回上一级");
            String choice = sc.nextLine();
            switch (choice) {
                case "1":
                    showScenicSpot();
                    break;
                case "2":
                    showTourRoute();
                    break;
                case "3":
                    showSpotById();
                    break;
                case "4":
                    showSpotByName();
                    break;
                case "5":
                    showTourRouteById();
                    break;
                case "6":
                    showTourRouteByName();
                    break;
                case "7":
                    showTourScenicrelation();
                    break;
                case "8":
                    showSpotNameTourRoute();
                    break;
                case "q":
                    bool = false;
                    break;
                default:
                    System.out.println("输入有误，请重新输入");
                    break;
            }
        } while (bool);
    }

    //添加信息方法
    public void addTour() {
        System.out.println("请选择功能：1、添加景点信息 2、添加旅游路线信息 q、返回上一级");
        String choice = sc.nextLine();
        switch (choice) {
            case "1":
                addScenicSpot();
                break;
            case "2":
                addTourRoute();
                break;
            case "q":
                break;
            default:
                System.out.println("输入有误!");
                break;
        }
    }

    //删除信息方法
    public void deleteTour() {
        System.out.println("请选择功能：1、删除景点信息 2、删除旅游路线信息 3、删除关系信息 q、返回上一级");
        String choice = sc.nextLine();
        switch (choice) {
            case "1":
                deleteScenicSpot();
                break;
            case "2":
                deleteTourRoute();
                break;
            case "3":
                deleteTourScenicrelation();
                break;
            case "q":
                break;
            default:
                System.out.println("输入有误!");
                break;
        }
    }

    //修改信息方法
    public void updateTour() {
        System.out.println("请选择功能：1、修改景点信息 2、修改旅游路线信息 3、修改关系表信息 q、返回上一级");
        String choice = sc.nextLine();
        switch (choice) {
            case "1":
                updateScenicSpot();
                break;
            case "2":
                updateTourRoute();
                break;
            case "3":
                System.out.println("功能暂未开放！！！");
                break;
            case "q":
                break;
            default:
                System.out.println("输入有误！");
                break;
        }
    }

    //查询所有景点信息
    public void showScenicSpot() {
        List<ScenicSpot> scenicSpotList = tourDao.selectScenicSpotAll(map);
        System.out.println("ID \t\t 景点名称 \t\t 推荐游玩时间 \t\t 景点介绍 \t\t");
        for (ScenicSpot spot : scenicSpotList) {
            System.out.println(spot.getId() + "\t\t" + spot.getName() + "\t\t" + spot.getDuration() + "\t\t" + spot.getContent());
        }
    }

    //查询所有旅游路线信息
    public void showTourRoute() {
        List<TourRoute> tourRouteList = tourDao.selectTourRouteAll(map);
        System.out.println("路线编号 \t\t 路线名称 \t\t 路线类型 \t\t 价格 \t\t 咨询电话 \t\t 咨询人 \t\t 创建日期");
        for (TourRoute tr : tourRouteList) {
            System.out.println(tr.getId() + "\t\t\t\t" + tr.getName() + "\t\t" + tr.getType() + "\t\t" + tr.getPrice() + "\t\t" + tr.getPhone() + "\t\t" + tr.getUser() + "\t\t" + tr.getCreateTime());
        }
    }

    //查询关系表所有信息
    public void showTourScenicrelation() {
        List<TourScenicrelation> tourScenicrelations = tourDao.selectTourScenicRelation(map);
        System.out.println("ID  \t\t SS_ID \t\t TR_ID");
        for (TourScenicrelation tsl : tourScenicrelations) {
            System.out.println(tsl.getId() + "\t\t" + tsl.getSsId() + "\t\t" + tsl.getTrId());
        }
    }

    //按ID查询景点信息
    public void showSpotById() {
        Map<String, Object> map = new HashMap<>();
        System.out.println("请输入需要查询的景点ID：");
        map.put("scenicId", sc.nextLine());
        List<ScenicSpot> scenicSpotList = tourDao.selectScenicSpotAll(map);
        System.out.println("ID \t\t 景点名称 \t\t 推荐游玩时间 \t\t 景点介绍 \t\t");
        for (ScenicSpot spot : scenicSpotList) {
            System.out.println(spot.getId() + "\t\t" + spot.getName() + "\t\t" + spot.getDuration() + "\t\t" + spot.getContent());
        }
    }

    //按名称查询景点信息
    public void showSpotByName() {
        Map<String, Object> map = new HashMap<>();
        List<ScenicSpot> scenicSpotNameList = tourDao.selectScenicSpotAll(map);
        System.out.println("库内已有景点名称：");
        for (ScenicSpot spotName : scenicSpotNameList) {
            System.out.print(spotName.getName() + "\t\t");
        }
        System.out.println();
        System.out.println("请输入需要查询的景点名称：");
        map.put("scenicName", sc.nextLine());
        List<ScenicSpot> scenicSpotList = tourDao.selectScenicSpotAll(map);
        System.out.println("ID \t\t 景点名称 \t\t 推荐游玩时间 \t\t 景点介绍 \t\t");
        for (ScenicSpot spot : scenicSpotList) {
            System.out.println(spot.getId() + "\t\t" + spot.getName() + "\t\t" + spot.getDuration() + "\t\t" + spot.getContent());
        }
    }

    //按ID查询旅游路线信息
    public void showTourRouteById() {
        Map<String, Object> map = new HashMap<>();
        System.out.println("请输入需要查询的旅游路线ID：");
        map.put("tourRouteId", sc.nextLine());
        List<TourRoute> tourRouteList = tourDao.selectTourRouteAll(map);
        System.out.println("路线编号 \t\t 路线名称 \t\t 路线类型 \t\t 价格 \t\t 咨询电话 \t\t 咨询人 \t\t 创建日期");
        for (TourRoute tr : tourRouteList) {
            System.out.println(tr.getId() + "\t\t\t\t" + tr.getName() + "\t\t" + tr.getType() + "\t\t" + tr.getPrice() + "\t\t" + tr.getPhone() + "\t\t" + tr.getUser() + "\t\t" + tr.getCreateTime());
        }
    }

    //按名称查询旅游路线信息
    public void showTourRouteByName() {
        Map<String, Object> map = new HashMap<>();
        List<TourRoute> tourRouteNameList = tourDao.selectTourRouteAll(map);
        System.out.println("库内已有路线名称：");
        for (TourRoute trName : tourRouteNameList) {
            System.out.print(trName.getName() + "\t\t");
        }
        System.out.println();
        System.out.println("请输入需要查询的旅游路线名称：");
        map.put("tourRouteName", sc.nextLine());
        List<TourRoute> tourRouteList = tourDao.selectTourRouteAll(map);
        System.out.println("路线编号 \t\t 路线名称 \t\t 路线类型 \t\t 价格 \t\t 咨询电话 \t\t 咨询人 \t\t 创建日期");
        for (TourRoute tr : tourRouteList) {
            System.out.println(tr.getId() + "\t\t\t\t" + tr.getName() + "\t\t" + tr.getType() + "\t\t" + tr.getPrice() + "\t\t" + tr.getPhone() + "\t\t" + tr.getUser() + "\t\t" + tr.getCreateTime());
        }
    }

    //通过景点名称查看旅游路线
    public void showSpotNameTourRoute() {
        Map<String, Object> mapIdName = new HashMap<>();
        List<ScenicSpot> scenicSpotNameList = tourDao.selectScenicSpotAll(mapIdName);
        System.out.println("库中已有景点信息:");
        int num = 1;
        for (ScenicSpot sc : scenicSpotNameList) {
            if (num % 5 == 0) {
                System.out.println();
            }
            System.out.print("ID:" + sc.getId() + "\t 名称:" + sc.getName() + "\t\t\t");
            num++;
        }
        System.out.println();
        System.out.println("请输入要查看的景点ID：");
        map.put("scenicId", sc.nextLine());
        List<ScenicSpot> scenicSpotList = tourDao.selectScenicSpotAll(map);
        for (ScenicSpot ss: scenicSpotList){
            map.put("tourRouteName",ss.getName());
        }
        List<TourRoute> tourRouteList = tourDao.selectTourRouteAll(map);
        System.out.println("路线编号 \t\t 路线名称 \t\t 路线类型 \t\t 价格 \t\t 咨询电话 \t\t 咨询人 \t\t 创建日期");
        for (TourRoute tr : tourRouteList) {
            System.out.println(tr.getId() + "\t\t\t\t" + tr.getName() + "\t\t" + tr.getType() + "\t\t" + tr.getPrice() + "\t\t" + tr.getPhone() + "\t\t" + tr.getUser() + "\t\t" + tr.getCreateTime());
        }
    }

    //添加景点信息方法
    public void addScenicSpot() {
        ScenicSpot scenicSpot = new ScenicSpot();
        System.out.println("请输入要添加的景点名称：");
        scenicSpot.setName(sc.nextLine());
        System.out.println("请输入添加景点的推荐游玩时间：");
        scenicSpot.setDuration(sc.nextLine());
        System.out.println("请输入添加景点的介绍：");
        scenicSpot.setContent(sc.nextLine());
        int addRes = tourDao.addScenicSpot(scenicSpot);
        System.out.println(addRes > 0 ? "添加成功！" : "添加失败！");
    }

    //添加旅游路线信息方法
    public void addTourRoute() {
        TourRoute tourRoute = new TourRoute();
        boolean bool = true;
        Map<String, Object> mapIdName = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        String name = "";

        //给关系表定义两个变量用于存储参数
        String ssId = "";
        String trId = "";

        System.out.println("请选择名称添加方式 1、手动添加 2、智能添加 ");
        String chose = sc.nextLine();
        switch (chose) {
            case "1":
                System.out.println("请输入旅游路线名称：");
                name = sc.nextLine();
                System.out.println("请输入景点ID：");
                ssId = sc.nextLine();
                break;
            case "2":
                do {
                    List<ScenicSpot> scenicSpotList = tourDao.selectScenicSpotAll(mapIdName);
                    System.out.println("库中已有景点信息:");
                    int num = 1;
                    for (ScenicSpot sc : scenicSpotList) {
                        if (num % 5 == 0) {
                            System.out.println();
                        }
                        System.out.print("ID:" + sc.getId() + "\t 名称:" + sc.getName() + "\t\t\t");
                        num++;
                    }
                    System.out.println();
                    System.out.println("请选择要添加的旅游景点ID：输入q结束添加");
                    String choice = sc.nextLine();
                    if ("q".equals(choice)) {
                        bool = false;
                    } else {
                        map.put("scenicId", choice);
                        if (ssId.length() == 0) {
                            ssId = choice;
                        } else {
                            ssId = ssId + "," + choice;
                        }
                        List<ScenicSpot> scenicSpotNameList = tourDao.selectScenicSpotAll(map);
                        for (ScenicSpot sc : scenicSpotNameList) {
                            if (name.length() == 0) {
                                name = sc.getName();
                            } else {
                                name = name + "," + sc.getName();
                            }
                        }
                    }
                } while (bool);
                break;
            default:
                System.out.println("输入有误！请重新输入");
                addTourRoute();
                break;
        }
        tourRoute.setName(name);
        System.out.println("请输入旅游路线类型：1:自由行；2：纯玩团");
        tourRoute.setType(sc.nextLine());
        System.out.println("请输入旅游路线价格：");
        tourRoute.setPrice(sc.nextLine());
        System.out.println("请输入旅游路线咨询电话：");
        tourRoute.setPhone(sc.nextLine());
        System.out.println("请输入旅游路线咨询人姓名：");
        tourRoute.setUser(sc.nextLine());
        int addRes = tourDao.addTourRoute(tourRoute);
        System.out.println(addRes > 0 ? "旅游路线信息添加成功！" : "旅游路线信息添加失败！");
        map.put("tourRouteName", name);
        List<TourRoute> tourRouteList = tourDao.selectTourRouteAll(map);
        for (TourRoute tr : tourRouteList) {
            trId = tr.getId();
        }
        addRes = tourDao.addTourScenicrelation(ssId, trId);
        System.out.println(addRes > 0 ? "关系表添加成功！" : "关系表添加失败！");
    }

    //删除景点信息方法
    public void deleteScenicSpot() {
        List<ScenicSpot> scenicSpotIdList = tourDao.selectScenicSpotAll(map);
        System.out.println("库内已有景点ID：");
        for (ScenicSpot spotName : scenicSpotIdList) {
            System.out.print(spotName.getId() + "\t\t");
        }
        System.out.println();
        System.out.println("请输入要删除景点信息的ID：");
        int deleteRes = tourDao.deleteScenicSpotById(sc.nextLine());
        System.out.println(deleteRes > 0 ? "删除成功！" : "删除失败！");
    }

    //删除旅游路线信息方法
    public void deleteTourRoute() {
        List<TourRoute> tourRouteIdList = tourDao.selectTourRouteAll(map);
        System.out.println("库内已有路线ID：");
        for (TourRoute trName : tourRouteIdList) {
            System.out.print(trName.getId() + "\t\t");
        }
        System.out.println();
        System.out.println("请输入要删除旅游路线信息的ID：");
        int deleteRes = tourDao.deleteTourRouteById(sc.nextLine());
        System.out.println(deleteRes > 0 ? "删除成功！" : "删除失败！");
    }

    //删除关系信息方法
    public void deleteTourScenicrelation() {
        List<TourScenicrelation> tourScenicrelations = tourDao.selectTourScenicRelation(map);
        System.out.println("库内已有ID：");
        for (TourScenicrelation tsl : tourScenicrelations) {
            System.out.print(tsl.getId() + "\t\t");
        }
        System.out.println();
        System.out.println("请输入要删除的信息ID：");
        int deleteRes = tourDao.deleteTourScenicRelation(sc.nextLine());
        System.out.println(deleteRes > 0 ? "删除成功！" : "删除失败！");
    }

    //修改景点信息方法
    public void updateScenicSpot() {
        ScenicSpot scenicSpot = new ScenicSpot();
        List<ScenicSpot> scenicSpotIdList = tourDao.selectScenicSpotAll(map);
        System.out.println("库内已有景点ID：");
        for (ScenicSpot spotName : scenicSpotIdList) {
            System.out.print(spotName.getId() + "\t\t");
        }
        System.out.println();
        System.out.println("请选择要修改信息的景点ID：");
        scenicSpot.setId(sc.nextLine());
        System.out.println("请输入要修改成的景点名称：");
        scenicSpot.setName(sc.nextLine());
        System.out.println("请输入要修改景点推荐游玩时长：");
        scenicSpot.setDuration(sc.nextLine());
        System.out.println("请输入景点介绍：");
        scenicSpot.setContent(sc.nextLine());
        int updateRes = tourDao.updateScenicSpot(scenicSpot);
        System.out.println(updateRes > 0 ? "修改成功！" : "修改失败！");
    }

    //修改旅游路线信息方法
    public void updateTourRoute() {
        TourRoute tourRoute = new TourRoute();
        boolean bool = true;
        Map<String, Object> mapIdName = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        String name = "";

        //给关系表定义两个变量用于存储参数
        String ssId = "";
        String trId = "";

        List<TourRoute> tourRouteIdList = tourDao.selectTourRouteAll(map);
        System.out.println("库内已有路线ID：");
        for (TourRoute trName : tourRouteIdList) {
            System.out.print(trName.getId() + "\t\t");
        }
        System.out.println();
        System.out.println("请输入要修改的旅游路线ID：");
        tourRoute.setId(sc.nextLine());
        do {
            List<ScenicSpot> scenicSpotList = tourDao.selectScenicSpotAll(mapIdName);
            System.out.println("库中已有景点信息:");
            int num = 1;
            for (ScenicSpot sc : scenicSpotList) {
                if (num % 5 == 0) {
                    System.out.println();
                }
                System.out.print("ID:" + sc.getId() + "\t 名称:" + sc.getName() + "\t\t\t");
                num++;
            }
            System.out.println();
            System.out.println("请选择要添加的旅游景点ID：输入q结束添加");
            String choice = sc.nextLine();
            if ("q".equals(choice)) {
                bool = false;
            } else {
                map.put("scenicId", choice);
                if (ssId.length() == 0) {
                    ssId = choice;
                } else {
                    ssId = ssId + "," + choice;
                }
                List<ScenicSpot> scenicSpotNameList = tourDao.selectScenicSpotAll(map);
                for (ScenicSpot sc : scenicSpotNameList) {
                    if (name.length() == 0) {
                        name = sc.getName();
                    } else {
                        name = name + "+" + sc.getName();
                    }
                }
            }
        } while (bool);
        tourRoute.setName(name);
        System.out.println("请输入旅游路线类型：1:自由行；2：纯玩团");
        tourRoute.setType(sc.nextLine());
        System.out.println("请输入旅游路线价格：");
        tourRoute.setPrice(sc.nextLine());
        System.out.println("请输入旅游路线咨询电话：");
        tourRoute.setPhone(sc.nextLine());
        System.out.println("请输入旅游路线咨询人姓名：");
        tourRoute.setUser(sc.nextLine());
        int updateRes = tourDao.updateTourRoute(tourRoute);
        System.out.println(updateRes > 0 ? "旅游路线信息修改成功！" : "旅游路线信息修改失败！");
        map.put("tourRouteName", name);
        List<TourRoute> tourRouteList = tourDao.selectTourRouteAll(map);
        for (TourRoute tr : tourRouteList) {
            trId = tr.getId();
        }
        updateRes = tourDao.addTourScenicrelation(ssId, trId);
        System.out.println(updateRes > 0 ? "关系表添加成功！" : "关系表添加失败！");
    }
}
